# coding:utf8

from appserver import db_store
from sqlalchemy import or_
from models.entity.model_base import ModelBase

"""
模块数据模型
"""
"""不要改了"""


class FacilityType(ModelBase):
    __tablename__ = "facility_type"
    __bind_key__ = "main"

    # autoincrement=True,
    id = db_store.Column(db_store.Integer, primary_key=True, comment=u'主键')
    name = db_store.Column(db_store.String(64), nullable=False, unique=True, comment=u'设备类型名称')
    code = db_store.Column(db_store.String(32), nullable=False, unique=True, comment=u"类型编码")
    description = db_store.Column(db_store.String(255), nullable=False, comment=u"描述")
    dis_order = db_store.Column(db_store.Integer, comment=u"排序",default=0)
    icon = db_store.Column(db_store.String(32), comment=u"图标")
    # is_bool = db_store.Column(db_store.Integer, comment=u"11")  # 0代表不是逻辑类型，1代表是逻辑类型
    facility = db_store.relationship('Facility', backref='facility', lazy='dynamic')

    def __repr__(self):
        return "<FacilityType %.2f>" % self.id

    @staticmethod
    def get_facility_type_by_name(name):
        return FacilityType.query.filter_by(name=name).first()

    @staticmethod
    def get_facility_type_by_code(code):
        return FacilityType.query.filter_by(code=code).first()

    @staticmethod
    def search(query_word):
        tmp = {}
        data = []
        facility_types = FacilityType.query.filter(
            or_(FacilityType.code.like('%' + query_word + '%'), FacilityType.name.like('%' + query_word + '%'),
                FacilityType.description.like('%' + query_word + '%'))).all()
        for obj in facility_types:
            data.append(obj.to_dict())
        tmp['success'] = 1
        tmp['error_code'] = None
        tmp['error_msg'] = None
        tmp['data'] = data
        return tmp
